'use strict';

const Controller = require('egg/index').Controller;

class AliController extends Controller {
  /**
   * OSS签名
   */
  async ossSign() {
    const data = await this.ctx.service.ali.ossSign();
    this.ctx.sendSuccess(data);
  }

  /**
   * 删除数据
   */
  async ossDelete() {
    this.ctx.validate({
      name: {
        type: 'string',
      },
      bucket: {
        type: 'string',
      },
    }, this.ctx.query);
    const { name, bucket } = this.ctx.query;
    const data = this.ctx.service.ali.ossDelete(name, bucket);
    this.ctx.sendSuccess(data);
  }

  /**
   * 获取文件列表
   */
  async ossList() {
    const {query, service} = this.ctx
    this.ctx.validate({
      prefix: {
        type: 'string',
      },
      lastKey: {
        type: 'string',
        required: false,
      },
      pageSize: {
        type: 'number',
        convertType: 'number',
        max: 100,
        min: 1,
      },
    }, query);
    const { prefix, lastKey, pageSize } = query;
    const data = await service.ali.ossList(prefix, lastKey, pageSize);
    this.ctx.sendSuccess(data);
  }
}

module.exports = AliController;
